﻿Imports System.Xml
Imports System.Reflection
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Workflow.BusinessDomain
Imports Mysoft.Map.Data
Imports System.Web


Public Class ProceedPlanReSP

    Inherits BusinessDomainBase

    Private _businessGUID As String     '业务GUID
    Private _xml As XmlDocument = New XmlDocument

    Public Sub New()
        MyBase.New("/PubProject/workflow/qmys/ProceedPlanReSP.xml")
        _xml.LoadXml(MyBase.XML)
    End Sub

    '属性：业务GUID
    Public Overrides Property BusinessGUID() As String
        Get
            Return _businessGUID
        End Get
        Set(ByVal Value As String)
            _businessGUID = Value
        End Set
    End Property

    '属性：XML
    '最终XML格式：

    Public Overrides Property XML() As String
        Get
            Return _xml.OuterXml
        End Get
        Set(ByVal Value As String)
            _xml.LoadXml(Value)
        End Set
    End Property

    Public Overrides Function Initiate() As String
        Dim strYear As String = MyDB.GetDataItemString("SELECT Year FROM ys_YearPlan2Object WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'")
        Dim strDeptGUID As String = MyDB.GetDataItemString("SELECT ObjectGUID FROM ys_YearPlan2Object WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'")
        Dim strCostGUID As String = MyDB.GetDataItemString("select * from ys_DeptCost where ys_DeptCost.year=" & strYear & " and ys_DeptCost.costlevel=1")
        Dim strSQL As String
        strSQL = "UPDATE ys_YearPlan2Object SET ReItemState='审核中',ReportedBy='" & HttpContext.Current.Session("UserName") & "',ReportDate=Convert(varchar(10),GetDate(),120)" & _
                 ",PlanAmountYear=(select sum(PlanAmount1+PlanAmount2+PlanAmount3+PlanAmount4+PlanAmount5+PlanAmount6+PlanAmount7+PlanAmount8+PlanAmount9+PlanAmount10+PlanAmount11+PlanAmount12+AdjustAmount1+AdjustAmount2+AdjustAmount3+AdjustAmount4+AdjustAmount5+AdjustAmount6+AdjustAmount7+AdjustAmount8+AdjustAmount9+AdjustAmount10+AdjustAmount11+AdjustAmount12) from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount1=(select PlanAmount1+AdjustAmount1 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount2=(select PlanAmount2+AdjustAmount2 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount3=(select PlanAmount3+AdjustAmount3 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount4=(select PlanAmount4+AdjustAmount4 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount5=(select PlanAmount5+AdjustAmount5 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount6=(select PlanAmount6+AdjustAmount6 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount7=(select PlanAmount7+AdjustAmount7 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount8=(select PlanAmount8+AdjustAmount8 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount9=(select PlanAmount9+AdjustAmount9 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount10=(select PlanAmount10+AdjustAmount10 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount11=(select PlanAmount11+AdjustAmount11 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 ",PlanAmount12=(select PlanAmount12+AdjustAmount12 from ys_YearPlanDept2Cost where costguid='" & strCostGUID & "' and deptguid='" & strDeptGUID & "' and year=" & strYear & ")" & _
                 " WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'"
        '提交更新语句
        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""上报失败，请稍后再试！""></xml>"
        End If

        '返回执行是否成功的信息
        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function
    '功能：取业务数据，填充XML
    Public Overrides Function LoadData() As Integer
        Dim strSQL As String
        Dim dtTemp As DataTable

        Dim strYear As String = MyDB.GetDataItemString("SELECT Year FROM ys_YearPlan2Object WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'")
        Dim strDeptGUID As String = MyDB.GetDataItemString("SELECT ObjectGUID FROM ys_YearPlan2Object WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'")
        Dim strCostGUID As String = MyDB.GetDataItemString("select * from ys_DeptCost where ys_DeptCost.year=" & strYear & " and ys_DeptCost.costlevel=1")

        '主记录    
        strSQL = "SELECT YearPlan2ObjectGUID, ObjectGUID,BUName, Year,(select PlanAmount1+PlanAmount2+PlanAmount3+PlanAmount4+PlanAmount5+PlanAmount6+PlanAmount7+PlanAmount8+PlanAmount9+PlanAmount10+PlanAmount11+PlanAmount12+AdjustAmount1+AdjustAmount2+AdjustAmount3+AdjustAmount4+AdjustAmount5+AdjustAmount6+AdjustAmount7+AdjustAmount8+AdjustAmount9+AdjustAmount10+AdjustAmount11+AdjustAmount12 from ys_YearPlanDept2Cost where year=" & strYear & " and deptguid='" & strDeptGUID & "' and costguid='" & strCostGUID & "') PlanAmountYear,ReportedBy, ReportDate " & _
                 " FROM ys_YearPlan2Object Left Join vys_myBusinessUnit On ys_YearPlan2Object.ObjectGUID = vys_myBusinessUnit.BUGUID " & _
                 " WHERE YearPlan2ObjectGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)
        If dtTemp.Rows.Count > 0 Then
            '根节点业务GUID属性
            _xml.SelectSingleNode("/BusinessType").Attributes.GetNamedItem("BusinessGUID").Value = Me._businessGUID
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='年度预算计划2部门GUID']").InnerText = dtTemp.Rows(0).Item("YearPlan2ObjectGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='部门GUID']").InnerText = dtTemp.Rows(0).Item("ObjectGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请部门']").InnerText = dtTemp.Rows(0).Item("BUName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='年度']").InnerText = dtTemp.Rows(0).Item("Year").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='上报金额']").InnerText = dtTemp.Rows(0).Item("PlanAmountYear").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='上报人名称']").InnerText = dtTemp.Rows(0).Item("ReportedBy").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='上报日期']").InnerText = dtTemp.Rows(0).Item("ReportDate").ToString

        End If

        Return 0
    End Function

    '功能：根据XML进行数据归档
    Public Overrides Function SaveData() As String
        Dim MyPublicMng As New PublicMNG
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator()
        Dim strSQL As String

        strSQL = "UPDATE ys_YearPlan2Object SET ReItemState=ItemState" & _
                    ",ApprovedBy='" & HttpContext.Current.Session("UserName") & "'" & _
                    ",ApproveDate=Convert(varchar(10),GetDate(),120)" & _
                    ",ApproveRemarks='" & strApproveSeparator & Chr(13) & Chr(10) & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(ApproveRemarks, '')) " & _
                    " WHERE YearPlan2ObjectGUID='" & Me._businessGUID & "';"


        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""归档失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

    '功能：不通过处理
    Public Overrides Function SaveData_NoPass() As String
        Dim MyPublicMng As New PublicMNG
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator()
        Dim strSQL As String
        strSQL = "UPDATE ys_YearPlan2Object SET ReItemState='未审核',ReportedBy='',ReportDate=NULL" & _
               ",ApproveRemarks='" & strApproveSeparator & Chr(13) & Chr(10) & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(ApproveRemarks, '')) " & _
               ",PlanAmountYear=0.00" & _
               ",PlanAmount1=0.00" & _
               ",PlanAmount2=0.00" & _
               ",PlanAmount3=0.00" & _
               ",PlanAmount4=0.00" & _
               ",PlanAmount5=0.00" & _
               ",PlanAmount6=0.00" & _
               ",PlanAmount7=0.00" & _
               ",PlanAmount8=0.00" & _
               ",PlanAmount9=0.00" & _
               ",PlanAmount10=0.00" & _
               ",PlanAmount11=0.00" & _
               ",PlanAmount12=0.00" & _
               " WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""操作失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"

    End Function
    '功能：作废
    Public Overrides Function SaveData_Cancel() As String
        Dim MyPublicMng As New PublicMNG
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator()
        Dim strSQL As String
        strSQL = "UPDATE ys_YearPlan2Object SET ReItemState='未审核',ReportedBy='',ReportDate=NULL" & _
              ",ApproveRemarks='" & strApproveSeparator & Chr(13) & Chr(10) & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(ApproveRemarks, '')) " & _
              ",PlanAmountYear=0.00" & _
              ",PlanAmount1=0.00" & _
              ",PlanAmount2=0.00" & _
              ",PlanAmount3=0.00" & _
              ",PlanAmount4=0.00" & _
              ",PlanAmount5=0.00" & _
              ",PlanAmount6=0.00" & _
              ",PlanAmount7=0.00" & _
              ",PlanAmount8=0.00" & _
              ",PlanAmount9=0.00" & _
              ",PlanAmount10=0.00" & _
              ",PlanAmount11=0.00" & _
              ",PlanAmount12=0.00" & _
              " WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""操作失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function


End Class
